import {observable, action, runInAction} from 'mobx';
import {Toast} from 'antd-mobile';

/**
 * mobx 状态管理类，所有的逻辑处理都在这里
 * */
class CarOrderStore {
    @observable orderInfo; //订单需要的信息
    @observable payments = 0;//付款金额
    @observable parkingTime = 0; //停车时长
    @observable state;
    @observable detail = '';
    @observable attach;
    @observable attach2;
    @observable commCode = ''; //小区编号

    @action
    setOrderInfo = (orderInfo, props) => {
        Toast.loading('正在获取停车数据...', 0);
        let params = {};
        runInAction(() => {
            params.parkId = this.attach2 = orderInfo.parkId;
            params.plateNumber = this.attach = orderInfo.plateNumber;
        });
        postJson("/api/v2/car/anon/queryCarFeeQueue", params, (data) => {
            console.log("data", data);
            runInAction(() => {
                let jsObject = JSON.parse(data);
                this.orderInfo = orderInfo;
                this.payments = jsObject.payments;
                this.parkingTime = jsObject.period;
                this.commCode = orderInfo.commCode;
            });
            Toast.hide();
        }, (error) => {
            Toast.hide();
            props.history.push({
                pathname: '/payAuth/jhcloud/payFail'
            }, {
                errorMsg: error.data
            });
        });
    };

    paySubmit = (props) => {
        console.log("orderInfo", this.orderInfo);
        let params = {
            payType: "wx_h5",
            body: '车辆预约出场',
            orderType: "CAR_ORDERED",
            totalFee: 0.01,
            commCode: this.commCode,
            payableFee: this.payments,
            code: this.orderInfo.code,
            attach: this.orderInfo.plateNumber,
            attach2: this.orderInfo.parkId
        };
        postJson("/api/v2/pay/anon/weixin/H5/order", params, (data) => {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', data,
                (res) => {
                    //使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                    if (res.err_msg === "get_brand_wcpay_request:ok") {
                        props.history.push({
                            pathname: '/payAuth/jhcloud/paySuccess',
                            orderInfo: {
                                plateNumber: this.orderInfo.plateNumber,
                                payments: this.payments,
                                creationTime: this.orderInfo.creationTime
                            }
                        });
                    } else {
                        props.history.push({
                            pathname: '/payAuth/jhcloud/payFail'
                        });
                    }
                }
            );
        }, (data) => {
            Toast.info(data.data ? data.data : data, 2, null, false);
        });
    };
}

export default new CarOrderStore()